﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using AzureGames.Data;
using AzureGames.Domain.Models;
using AzureGames.Domain.Contracts;

namespace AzureGames.Domain.Handlers
{
    public class GetGameDefinitionList
    {
        private readonly IGameDefinitionRepository _gameDefinitionRepository;

        public GetGameDefinitionList(IGameDefinitionRepository gameDefinitionRepository)
        {
            _gameDefinitionRepository = gameDefinitionRepository;
        }

        public virtual IEnumerable<GameDefinition> Execute()
        {
            IEnumerable<Model.GameDefinition> gameDefs = null;

            try
            {
                gameDefs = _gameDefinitionRepository.GetGameDefinitions();
            }
            catch (InvalidOperationException e)
            {
                throw new BusinessServicesException("Unable to retrieve game definition from database.", e);

            }

            var games = from def in gameDefs
                        select new GameDefinition(def);

            return games;
        }
    }
}